﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;

namespace Test1
{
    public class Class1
    {
        [CommandMethod("HelloWorld")]
        public void HelloWorld()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("Hello World");
        }

        [CommandMethod("Create")]
        public void Create()
        {
            //DBText text = addText("ddddd", new Point3d(), true);
            //AppendEntity(text);
            creareTextStyle("dd");
        }

        private void drawTri(Point3d point, BlockTableRecord btr, Transaction trans)
        {
            Point3d pt1 = point;
            Point3d pt2 = new Point3d(point.X - 1.0, point.Y + 1.5, 0);
            Point3d pt3 = new Point3d(point.X + 1.0, point.Y + 1.5, 0);
            Line line1 = new Line(pt1, pt2);
            Line line2 = new Line(pt1, pt3);
            Line line3 = new Line(pt2, pt3);
            btr.AppendEntity(line1);
            trans.AddNewlyCreatedDBObject(line1, true);
            btr.AppendEntity(line2);
            trans.AddNewlyCreatedDBObject(line2, true);
            btr.AppendEntity(line3);
            trans.AddNewlyCreatedDBObject(line3, true);
        }

        private DBText addText(string text, Point3d position, bool isRight)
        {
            DBText dbText = new DBText();
            dbText.TextString = text;
            dbText.Height = 2.5;
            if (isRight)
            {
                dbText.Position = new Point3d(position.X + 2.0, position.Y + 1.0, 0);
            }
            else
            {
                dbText.Position = new Point3d(position.X + 2.0 - text.Length * 2.5 * 0.7, position.Y + 1.0, 0);
            }
            dbText.WidthFactor = 0.7;
            return dbText;
        }

        private ObjectId AppendEntity(Entity ent)
        {
            ObjectId objectId = new ObjectId(); //它返回函数的值
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
                objectId = btr.AppendEntity(ent);
                trans.AddNewlyCreatedDBObject(ent, true);
                trans.Commit();
            }
            catch
            {
                ed.WriteMessage("Error ");
            }
            finally
            {
                trans.Dispose();
            }
            return objectId;
        }

        /// <summary>
        /// 创建层
        /// </summary>
        /// <param name="layerName">层的名字</param>
        /// <param name="colorIndex">层的颜色</param>
        /// <returns></returns>
        private ObjectId creareLayer(string layerName, short colorIndex)
        {
            ObjectId layerId; //它返回函数的值
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();
            //首先取得层表……
            LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
            LayerTableRecord ltr;
            //检查layerName层是否存在……
            if (lt.Has(layerName))
            {
                layerId = lt[layerName];
                //设置层的颜色为colorIndex
                ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForWrite);
                ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex);
            }
            else
            {
                //如果layerName层不存在，创建layerName层
                ltr = new LayerTableRecord();
                ltr.Name = layerName; //设置层的名字
                ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex);//设置层的颜色
                layerId = lt.Add(ltr);
                trans.AddNewlyCreatedDBObject(ltr, true);
            }
            trans.Commit();
            trans.Dispose();
            return layerId;
        }

        private ObjectId creareTextStyle(string styleName)
        {
            ObjectId textStyleId;
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();
            TextStyleTable st = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
            TextStyleTableRecord str;
            if (st.Has(styleName))
            {
                textStyleId = st[styleName];
                str = (TextStyleTableRecord)trans.GetObject(textStyleId, OpenMode.ForWrite);
                str.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("宋体", false, false, 0x86, 2);
                str.XScale = 0.7;
            }
            else
            {
                //
                str = new TextStyleTableRecord();
                str.Name = styleName; //
                str.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("宋体", false, false, 0x86, 2);
                str.XScale = 0.7;
                textStyleId = st.Add(str);
                trans.AddNewlyCreatedDBObject(str, true);
            }
            db.Textstyle = textStyleId;
            trans.Commit();
            trans.Dispose();
            return textStyleId;
        }
    }
}
